package com.hwadee.property.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hwadee.property.pojo.Parking;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository
@Mapper
public interface ParkingMapper extends BaseMapper<Parking> {

    /**
     * 计算总记录数
     */
    @Select("<script>" +
            "SELECT COUNT(*) FROM parking" +
            "<where>" +
            "   <if test='communityId != null'> AND community_id = #{communityId} </if>" +
            "   <if test='location != null and location.trim() != \"\"'> " +
            "       AND location LIKE CONCAT('%', #{location}, '%') " +
            "   </if> " +
            "   <if test='status != null and status.trim() != \"\"'> " +
            "       AND status LIKE CONCAT('%', #{status}, '%') " +
            "   </if> " +
            "   <if test='createTime != null'> AND create_time = #{createTime} </if>" +
            "   <if test='updateTime != null'> AND update_time = #{updateTime} </if>" +
            "</where>" +
            "</script>")
    int countParking(@Param("communityId") Long communityId,
                     @Param("location") String location,
                     @Param("status") String status,
                     @Param("createTime") Date createTime,
                     @Param("updateTime") Date updateTime);

    /**
     * 分页查询车位
     */
    @Select("<script>" +
            "SELECT * FROM parking " +
            "<where>" +
            "   <if test='communityId != null'> AND community_id = #{communityId} </if>" +
            "   <if test='location != null and location.trim() != \"\"'> " +
            "       AND location LIKE CONCAT('%', #{location}, '%') " +
            "   </if> " +
            "   <if test='status != null and status.trim() != \"\"'> " +
            "       AND status LIKE CONCAT('%', #{status}, '%') " +
            "   </if> " +
            "   <if test='createTime != null'> AND create_time = #{createTime} </if>" +
            "   <if test='updateTime != null'> AND update_time = #{updateTime} </if>" +
            "</where>" +
            "ORDER BY id ASC " +
            "LIMIT #{size} OFFSET #{offset}" +
            "</script>")
    List<Parking> getParkingList(@Param("communityId") Long communityId,
                                 @Param("location") String location,
                                 @Param("status") String status,
                                 @Param("createTime") Date createTime,
                                 @Param("updateTime") Date updateTime,
                                 @Param("size") int size,
                                 @Param("offset") int offset);

}
